import { checkShop } from "@/apis/shop"
import { checkBill } from "@/apis/bill"
import { batchAddOrder } from "@/apis/order"
import dayjs from "dayjs"
export const format = {
	"id": "orderId",
	"账单编号": "bid",
	"店铺名称": "shopName",
	"所属平台": "platform",
	"每单结算金额": "money",
	"活动日期": "activeDate",
	"微信昵称": "nickname",
	"订单号": "orderNo",
	"手机尾号": "phone",
	"实付金额": "amount",
	"订单截图1": "imgList",
	"订单截图2": "imgList",
	"反馈截图": "imgList",
	"反馈截图1": "imgList",
	"反馈截图2": "imgList",
	"反馈截图3": "imgList",
	"订单创建时间": "orderCreateTime",
	//---------------------
	"店名":"shopName",
	"平台":"platform",
	"结算金额": "money",
	"日期": ["activeDate","orderCreateTime"],
	"用户实付": "amount",
	"单号": ["orderNo","orderId"],
	"反馈图片": "imgList",
	"订单图片": "imgList",
	"订单图片_1": "imgList",
}

export function cnKeyJson2en(arr){
	let result = []
	arr.forEach(item=>{
		let obj = {
			imgList:[]
		}
		for (const key in format) {
			if(item[key] && item[key] != "暂无"){
				if(format[key] instanceof Array){
					format[key].forEach(f=>{
						genVal(obj,f,item[key])
					})
				}else{
					genVal(obj,format[key],item[key])
					// if(obj[format[key]] instanceof Array){
					// 	obj[format[key]].push(item[key])
					// }else if(key == "活动日期"){
					// 	obj[format[key]] = dayjs(item[key]).format("YYYY-MM-DD")
					// }
					// else{
					// 	obj[format[key]] = item[key]
					// }
				}
				
			}
		}
		result.push(obj)
	})
	return result
}
function genVal(obj,key,value){
	if(obj[key] instanceof Array){
		obj[key].push(value)
	}else if(key == "activeDate"){
		obj[key] = dayjs(value).format("YYYY-MM-DD")
	}else if(key == "platform"){
		if(value.includes("美团")){
			obj[key] = "美团外卖"
		}else if(value.includes("京东秒送")){
			obj[key] = "京东秒送"
		}else{
			obj[key] = "饿了么"
		}
	}
	else{
		obj[key] = value
	}
}

export const columns = {
	shop:[
		{
			key:"shopName",
			label:"店铺名",
			// width:false,
			slot:"shopName",
			props:{
				"show-overflow-tooltip":true
			},
		},
		{
			key:"total",
			label:"总金额",
			// width:false,
			headerSlot:"total",
			props:{
				"show-overflow-tooltip":true
			},
		},	
		{
			key:"platform",
			label:"来源平台",
			headerSlot:"platform"
		},
		{
			key:"count",
			label:"订单数",
			headerSlot:"count"
		},
		{
			key:"prices",
			label:"价格",
			slot:"prices"
		},
		{
			key:"operation",
			label:"操作",
			slot:"operation",
			width:350,
			props:{
				fixed:"right"
			},
			removeBtn:{
				show:true,
			},
			editBtn:{
				show:false,
			},
		}
	],
	order:[
		{
			key:"expand",
			props:{
				type:"expand"
			},
			slot:"expand"
		},
		{
			key:"orderNo",
			label:"订单号",
			// width:200,
		},
		{
			key:"money",
			label:"每单结算金额",
			// width:120,
		},
		{
			key:"shopName",
			label:"店铺名",
			// width:200,
			props:{
				"show-overflow-tooltip":true
			}
		},
		{
			key:"nickname",
			label:"微信昵称",
			// width:200,
		},
		{
			key:"phone",
			label:"手机尾号",
			// width:200,
		},
		{
			key:"amount",
			label:"实付金额",
			// width:200,
		},
		{
			key:"orderCreateTime",
			label:"订单创建时间",
			// width:120,
		},
		{
			key:"operation",
			label:"操作",
			slot:"operation",
			// width:150,
			props:{
				fixed:"right"
			},
			removeBtn:{
				show:true,
			},
			editBtn:{
				show:true,
			},
		}
    
	],
}

export const updated = {}

export const importToSys = async (shopArray,billArray,orders,config={})=>{
	await checkShop(shopArray,config)
	await checkBill(billArray,config)
	await batchAddOrder(orders,config)
}